//
//  SACollectionPayVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/8/26.
//

#import "SACollectionPayVC.h"
#import "SASearchVipUserView.h"
#import "SAVipModel.h"
#import "SAPayDocumentsVC.h"
#import "SACollectionPayView.h"
#import "SAPayCombinationVC.h"
#import "ScanHelper.h"
#import "SAReceiveSaveAPI.h"
#import "SAReceiveReceivePayAPI.h"
#import "SAReceiveQueryStatusAPI.h"
#import "GFAlertView.h"
#import "SAGetPayPermissionAPI.h"
#import "NSString+nonce.h"
#import "NSString+MD5.h"
#import "SASystemSettingsListAPI.h"

@interface SACollectionPayVC ()<HTTPAPIDelegate>

@property (nonatomic, strong) UIButton *vipBtn;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) SAVipModel *vipModel;
@property (nonatomic, assign) BOOL isVip;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UIButton *boardDeleteBtn;
@property (nonatomic, strong) UIButton *boardCloseBtn;
@property (nonatomic, strong) UIButton *boardAddBtn;
@property (nonatomic, strong) UIButton *boardDotBtn;

@property (nonatomic, strong) UILabel *priceLab2;
@property (nonatomic, strong) UILabel *priceLab3;
@property (nonatomic, strong) NSString *priceStr3;

@property (nonatomic, assign) BOOL isAdd;
@property (nonatomic, assign) BOOL isScan;
@property (nonatomic, strong) NSString *payType;
@property (nonatomic, strong) SAReceiveSaveAPI *receiveSaveAPI;

@property (nonatomic, assign) BOOL isScanPay;
@property (nonatomic, strong) NSString *receiveOrderId;
@property (nonatomic, strong) NSString *receiveOrderNo;
@property (nonatomic, strong) SAReceiveReceivePayAPI *receiveReceivePayAPI;
@property (nonatomic, strong) SAReceiveQueryStatusAPI *receiveQueryStatusAPI;

@property (nonatomic, assign) BOOL isPolling;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int count;

@property (nonatomic, strong) SAGetPayPermissionAPI *getPayPermissionAPI;
@property (nonatomic, assign) BOOL isCanPay;
@property (nonatomic, strong) UIButton *settlementBtn;
@property (nonatomic, strong) NSString *randomStr;

@property (nonatomic, strong) NSString *defaultPayType;//支付类型
@property (nonatomic, strong) SASystemSettingsListAPI *systemSettingsListAPI;

@end

@implementation SACollectionPayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.receiveSaveAPI == api) {
        if (self.isScanPay == YES) {
            NSInteger status = [data[@"data"][@"status"] integerValue];
            if (status == 2) {
                [self keyboardButtonAction:self.boardDeleteBtn];
                [Prompt popPromptViewWithMsg:@"收款成功" duration:2];
            }else if (status == 0) {
                self.receiveOrderId = data[@"data"][@"receiveOrderId"];
                self.receiveOrderNo = data[@"data"][@"receiveOrderNo"];
                [self scanQrcode];
            }
        }else {
            [self keyboardButtonAction:self.boardDeleteBtn];
            [Prompt popPromptViewWithMsg:@"收款成功" duration:2];
        }
    }else if (self.receiveReceivePayAPI == api) {
        NSInteger status = [data[@"status"] integerValue];
        if (status == 2) {
            [self keyboardButtonAction:self.boardDeleteBtn];
            [Prompt popPromptViewWithMsg:@"支付成功" duration:2];
        }else if (status == 0) {
            self.isPolling = YES;
            self.count = 31;
            [HDHubView loadingMessage:@"正在查询支付状态..."];
            self.receiveQueryStatusAPI.orderNo = self.receiveOrderNo;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
        }else {
            [self tipAlertView:@"支付失败"];
        }
    }else if (self.receiveQueryStatusAPI == api) {
        if (self.isPolling == YES) {
            NSInteger status = [data[@"data"][@"status"] integerValue];
            if (status == 2) {
                self.isPolling = NO;
                [self.timer invalidate];
                self.timer = nil;
                [HDHubView dismissLoading];
                [self keyboardButtonAction:self.boardDeleteBtn];
                [Prompt popPromptViewWithMsg:@"支付成功" duration:2];
            }else if (status == 6 || status == 8) {
                self.isPolling = NO;
                [self.timer invalidate];
                self.timer = nil;
                [HDHubView dismissLoading];
                [self tipAlertView:@"支付失败"];
            }
        }
    }else if (self.getPayPermissionAPI == api) {
        NSString *isCanPayStr = [NSString stringWithFormat:@"%@",data[@"data"]];
        if ([isCanPayStr isEqualToString:@"0"]) {
            self.isCanPay = NO;
        }else {
            self.isCanPay = YES;
        }
    }else if (self.systemSettingsListAPI == api) {
        self.defaultPayType = data[@"data"][@"priority_payment_method"][@"value"];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    if (self.receiveQueryStatusAPI == api) {
        if (self.isPolling == YES) {
            self.isPolling = NO;
            [self.timer invalidate];
            self.timer = nil;
            [HDHubView dismissLoading];
            NSDictionary *dic = error.userInfo;
            NSString *msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [self tipAlertView:msg];
        }
    }else if (self.receiveReceivePayAPI == api) {
        NSDictionary *dic = error.userInfo;
        NSString *msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        [self tipAlertView:msg];
    }else if (self.getPayPermissionAPI == api) {
        self.isCanPay = NO;
    }else if (self.receiveSaveAPI == api) {
        NSDictionary *dict = error.userInfo;
        if ([[NSString stringWithFormat:@"%@",dict[@"code"]] isEqualToString:@"50001"]) {
            NSString *msg = dict[@"msg"];
            [Prompt popPromptViewWithMsg:msg duration:3];
            [self keyboardButtonAction:self.boardDeleteBtn];
        }else if ([[NSString stringWithFormat:@"%@",dict[@"code"]] isEqualToString:@"50002"]) {
            NSString *msg = dict[@"msg"];
            [Prompt popPromptViewWithMsg:msg duration:3];
            [self keyboardButtonAction:self.boardDeleteBtn];
        }
    }
}

#pragma mark - event
-(void)clickedListBtn {
    SAPayDocumentsVC *pushVC = [SAPayDocumentsVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedVipBtn {
    SASearchVipUserView *searchVipUserView = [SASearchVipUserView shareCornView];
    UIViewController *vc = nil;
    [searchVipUserView showView:vc];
    WeakSelf
    searchVipUserView.selectBlock = ^(SAVipModel * _Nonnull vipModel) {
        weakSelf.vipModel = vipModel;
        [weakSelf.vipBtn setTitle:[NSString stringWithFormat:@"%@(%@)",vipModel.memberName,vipModel.mobile] forState:UIControlStateNormal];
        [weakSelf.vipBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [weakSelf.vipBtn setImage:[UIImage imageNamed:@"Sale_rightArrow"] forState:UIControlStateNormal];
        CGFloat imageWidth = weakSelf.vipBtn.imageView.bounds.size.width;
        CGFloat labelWidth = weakSelf.vipBtn.titleLabel.bounds.size.width;
        weakSelf.vipBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        weakSelf.vipBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
        weakSelf.isVip = YES;
        weakSelf.cancelBtn.hidden = NO;
    };
}

-(void)clickedCancelBtn {
    self.cancelBtn.hidden = YES;
    self.isVip = NO;
    self.vipModel = nil;
    [self.vipBtn setTitle:@"请选择会员" forState:UIControlStateNormal];
    [self.vipBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
    CGFloat imageWidth = self.vipBtn.imageView.bounds.size.width;
    CGFloat labelWidth = self.vipBtn.titleLabel.bounds.size.width;
    self.vipBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
    self.vipBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
}

//防止多次结单
- (void)unisversalButtonAcrion:(UIButton *)sender
{
    sender.userInteractionEnabled = NO;
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(clickedSettlementBtn) object:sender];
    [self performSelector:@selector(clickedSettlementBtn) withObject:sender afterDelay:0.2f];
}

-(void)clickedSettlementBtn {
    if (self.priceLab.text.length == 0) {
        self.settlementBtn.userInteractionEnabled = YES;
        [Prompt popPromptViewWithMsg:@"请先输入收款金额" duration:2];
        return;
    }
    WeakSelf
    BOOL isScan = self.isScan;
    if ([self.priceLab.text doubleValue] == 0) {
        isScan = NO;
    }
    if (self.isCanPay == NO) {
        isScan = NO;
    }
    SACollectionPayView *payView = [[SACollectionPayView alloc] initWithTotalPrice:self.priceLab.text AndIsVip:self.isVip IsScan:isScan PayType:self.defaultPayType];
    [payView showView:self];
    self.settlementBtn.userInteractionEnabled = YES;
    
    payView.settlementBlock = ^(NSString *payType) {
        weakSelf.payType = payType;
        [weakSelf saveData];
    };
    
    payView.combinationBlock = ^{
        NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
        NSMutableDictionary *mdic2 = [NSMutableDictionary dictionary];
        if (weakSelf.isVip == YES) {
            mdic2[@"memberId"] = @"1";
        }
        mdic[@"memberDataBean"] = mdic2;
        if (isScan == YES) {
            mdic[@"scan"] = @"1";
        }
        mdic[@"actualPrice"] = [NSString stringWithFormat:@"%0.lf",[self.priceLab.text doubleValue] * 100];
        SAPayCombinationVC *pushVC = [[SAPayCombinationVC alloc] initWithMdic:mdic];
        [weakSelf.navigationController pushViewController:pushVC animated:YES];
        pushVC.settlementBlock = ^(NSMutableDictionary * _Nonnull mdic, NSMutableArray * _Nonnull payMarr) {
            [weakSelf saveDataCombination:payMarr];
        };
    };
}

- (void)keyboardButtonAction:(UIButton *)button {
    if (self.boardDeleteBtn == button) {
        self.priceLab2.text = @"";
        self.priceStr3 = @"";
        self.priceLab3.text = @"0 +";
        self.priceLab2.hidden = YES;
        self.priceLab3.hidden = YES;
        self.isAdd = NO;
        self.randomStr = [NSString random:10];
    }else if (self.boardCloseBtn == button) {
        if (self.priceLab2.text.length < 2) {
            self.priceLab2.text = @"";
        }else {
            self.priceLab2.text = [self.priceLab2.text substringToIndex:self.priceLab2.text.length - 1];
        }
    }else if (self.boardAddBtn == button) {
        self.isAdd = YES;
        self.priceStr3 = self.priceLab.text;
        self.priceLab3.text = [NSString stringWithFormat:@"%@ +",self.priceLab.text];
        self.priceLab2.hidden = NO;
        self.priceLab3.hidden = NO;
        self.priceLab2.text = @"";
    }else if (self.boardDotBtn == button) {
        if ([self.priceLab2.text rangeOfString:@"."].location != NSNotFound) {
            return;
        }
        if (self.priceLab2.text.length == 0) {
            self.priceLab2.text = @"0.";
        }else {
            self.priceLab2.text = [NSString stringWithFormat:@"%@%@",self.priceLab2.text,@"."];
        }
    }else {
        if ([self.priceLab2.text rangeOfString:@"."].location != NSNotFound) {
            if ([self.priceLab2.text componentsSeparatedByString:@"."].lastObject.length >=2 ) {
                return;
            }
        }
        NSString *newStr = [NSString stringWithFormat:@"%@%@",self.priceLab2.text,button.titleLabel.text];
        if ([newStr doubleValue] + [self.priceStr3 doubleValue] < 100000000) {
            self.priceLab2.text = newStr;
        }
    }
    if (self.isAdd == YES) {
        NSInteger i = 0;
        if ([self.priceLab2.text rangeOfString:@"."].location != NSNotFound) {
            if ([self.priceLab2.text componentsSeparatedByString:@"."].lastObject.length > i) {
                i = [self.priceLab2.text componentsSeparatedByString:@"."].lastObject.length;
            }
        }
        if ([self.priceStr3 rangeOfString:@"."].location != NSNotFound) {
            if ([self.priceStr3 componentsSeparatedByString:@"."].lastObject.length > i) {
                i = [self.priceStr3 componentsSeparatedByString:@"."].lastObject.length;
            }
        }
        if (i == 0) {
            self.priceLab.text = [NSString stringWithFormat:@"%0.lf",[self.priceStr3 doubleValue] + [self.priceLab2.text doubleValue]];
        }else if (i == 1) {
            self.priceLab.text = [NSString stringWithFormat:@"%0.1lf",[self.priceStr3 doubleValue] + [self.priceLab2.text doubleValue]];
        }else {
            self.priceLab.text = [NSString stringWithFormat:@"%0.2lf",[self.priceStr3 doubleValue] + [self.priceLab2.text doubleValue]];
        }
    }else {
        self.priceLab.text = self.priceLab2.text;
    }
}

-(void)scanQrcode {
    WeakSelf
    __block NSString *receiveOrderId = self.receiveOrderId;
    __block NSString *receiveOrderNo = self.receiveOrderNo;
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:weixinStyle qrResultCallBack:^(id result) {
        weakSelf.receiveReceivePayAPI.authCode = result;
        weakSelf.receiveReceivePayAPI.receiveOrderId = receiveOrderId;
        weakSelf.receiveReceivePayAPI.receiveOrderNo = receiveOrderNo;
        [weakSelf.receiveReceivePayAPI sendRequestToServerWithActivityViewAndMask:POST];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

-(void)saveData {
    self.isScanPay = [self.payType isEqualToString:@"7"];
    if (self.isScanPay == YES) {
        if ([self.priceLab.text doubleValue] > 10000000) {
            [Prompt popPromptViewWithMsg:@"金额超出支付限额" duration:2];
            return;
        }
    }
    NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
    mdic[@"amount"] = [NSString stringWithFormat:@"%0.lf",[self.priceLab.text doubleValue] * 100];
    mdic[@"deviceId"] = kDeviceId;
    NSMutableArray *marr = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"payPrice"] = mdic[@"amount"];
    dic[@"payType"] = self.payType;
    [marr addObject:dic];
    mdic[@"payForms"] = marr;
    if (self.isVip == YES) {
        mdic[@"memberName"] = self.vipModel.memberName;
        mdic[@"memberId"] = self.vipModel.memberId;
    }
    
    NSData *nData = [NSJSONSerialization dataWithJSONObject:mdic options:0 error:nil];
    mdic[@"randomStr"] = [NSString stringWithFormat:@"%@%@",self.randomStr,[NSString getMD5WithData:nData]];
    
    self.receiveSaveAPI.coverParameters = mdic;
    [self.receiveSaveAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(void)saveDataCombination:(NSMutableArray *)payMarr {
    self.isScanPay = NO;
    double price = 0.0;
    for (NSDictionary *dic in payMarr) {
        if ([dic[@"payType"] isEqualToString:@"7"]) {
            self.isScanPay = YES;
            price = [dic[@"payPrice"] doubleValue]/100;
        }
    }
    if (self.isScanPay == YES) {
        if (price > 10000000) {
            [Prompt popPromptViewWithMsg:@"金额超出支付限额" duration:2];
            return;
        }
    }
    NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
    mdic[@"amount"] = [NSString stringWithFormat:@"%0.lf",[self.priceLab.text doubleValue] * 100];
    mdic[@"deviceId"] = kDeviceId;
    mdic[@"payForms"] = payMarr;
    if (self.isVip == YES) {
        mdic[@"memberName"] = self.vipModel.memberName;
        mdic[@"memberId"] = self.vipModel.memberId;
    }
    self.receiveSaveAPI.coverParameters = mdic;
    [self.receiveSaveAPI sendRequestToServerWithActivityViewAndMask:POST];
}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        [self.receiveQueryStatusAPI sendRequestToServerWithType:POST];
    } else {
        self.isPolling = NO;
        // 停掉定时器
        [self.timer invalidate];
        self.timer = nil;
        [HDHubView dismissLoading];
    }
}

-(void)tipAlertView:(NSString *)msg {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:msg knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn setTitle:@"重试" forState:UIControlStateNormal];
        [knowBtn addTarget:self action:@selector(continuePay) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)continuePay {
    [self scanQrcode];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"收款"];
    [self setBgImage:nil];
    self.view.backgroundColor = kLineColor;
    self.randomStr = [NSString random:10];
    
    UIButton *listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    listBtn.frame = CGRectMake(0, 0, kScaleNum(60), kScaleNum(44));
    [listBtn setImage:[UIImage imageNamed:@"shopSale_list"] forState:UIControlStateNormal];
    [listBtn addTarget:self action:@selector(clickedListBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addRightViews:@[listBtn]];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 45)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    NSArray *nameArr = @[@"会员"];
    NSArray *imageArr = @[@"Sale_vip"];
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        [titleBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        titleBtn.frame = SARectMake(20, StatusHeight + i * 45, 120, 45);
        titleBtn.titleLabel.font = kFont(15);
        [titleBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.view addSubview:titleBtn];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setImage:[UIImage imageNamed:@"Sale_rightArrow"] forState:UIControlStateNormal];
        rightBtn.frame = SARectMake(0, StatusHeight + i * 45, kBaseScreenWidth - 20, 45);
        rightBtn.titleLabel.font = kFont(15);
        [rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.view addSubview:rightBtn];
        [rightBtn setTitle:@"请选择会员" forState:UIControlStateNormal];
        [rightBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
        self.vipBtn = rightBtn;
        [rightBtn addTarget:self action:@selector(clickedVipBtn) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat imageWidth = rightBtn.imageView.bounds.size.width;
        CGFloat labelWidth = rightBtn.titleLabel.bounds.size.width;
        rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
    }
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setImage:[UIImage imageNamed:@"btn_cancle"] forState:UIControlStateNormal];
    cancelBtn.frame = SARectMake(kBaseScreenWidth - 37, StatusHeight, 30, 45);
    cancelBtn.backgroundColor = [UIColor whiteColor];
    [cancelBtn addTarget:self action:@selector(clickedCancelBtn) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.hidden = YES;
    self.cancelBtn = cancelBtn;
    [self.view addSubview:cancelBtn];
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight + 50, kBaseScreenWidth, 120)];
    whiteView2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView2];
    
    UILabel *priceLab = [[UILabel alloc] initWithFrame:SARectMake(0, StatusHeight + 50, kBaseScreenWidth - 10, 120)];
    priceLab.text = @"";
    priceLab.textColor = kPinkColor;
    priceLab.textAlignment = NSTextAlignmentRight;
    priceLab.font = kFont(30);
    [self.view addSubview:priceLab];
    self.priceLab = priceLab;
    
    UILabel *priceLab2 = [[UILabel alloc] init];
    priceLab2.text = @"";
    priceLab2.textColor = kBlackColor;
    priceLab2.textAlignment = NSTextAlignmentRight;
    priceLab2.font = kFont(20);
    [self.view addSubview:priceLab2];
    self.priceLab2 = priceLab2;
    [priceLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kScaleNum(StatusHeight + 60));
        make.right.equalTo(whiteView2.mas_right).offset(kScaleNum(-10));
    }];
    
    UILabel *priceLab3 = [[UILabel alloc] init];
    priceLab3.text = @"0 +";
    priceLab3.textColor = kBlackColor;
    priceLab3.textAlignment = NSTextAlignmentRight;
    priceLab3.font = kFont(20);
    [self.view addSubview:priceLab3];
    self.priceLab3 = priceLab3;
    [priceLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kScaleNum(StatusHeight + 60));
        make.right.equalTo(priceLab2.mas_left).offset(kScaleNum(-4));
    }];
    self.priceStr3 = 0;
    
    self.priceLab2.hidden = YES;
    self.priceLab3.hidden = YES;
    
    [self createKeyboardUI];
    self.isScan = [[SAValueManger shareInStance].userInfo.auditStatus isEqualToString:@"1"];
    BOOL isPayChannel = ![[SAValueManger shareInStance].userInfo.payChannel isEqualToString:@"2"];
    if (self.isScan == YES) {
        self.isScan = isPayChannel;
    }
    
    self.isCanPay = NO;
    [self.getPayPermissionAPI sendRequestToServerWithActivityViewAndMask:POST];
    [self.systemSettingsListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(void)createKeyboardUI {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *boardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            boardBtn.frame = SARectMake(j * 95, StatusHeight + 175 + i * 85, 90, 80);
            boardBtn.titleLabel.font = kFont(25);
            boardBtn.backgroundColor = [UIColor whiteColor];
            [boardBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            boardBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            boardBtn.layer.cornerRadius = kScaleNum(5);
            boardBtn.layer.masksToBounds = YES;
            NSString *titleStr = [NSString stringWithFormat:@"%d",i * 3 + j + 1];
            [boardBtn setTitle:titleStr forState:UIControlStateNormal];
            [self.view addSubview:boardBtn];
            [boardBtn addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    UIButton *boardBtn0 = [UIButton buttonWithType:UIButtonTypeCustom];
    boardBtn0.frame = SARectMake(0, StatusHeight + 175 + 3 * 85, 185, 80);
    boardBtn0.titleLabel.font = kFont(25);
    boardBtn0.backgroundColor = [UIColor whiteColor];
    [boardBtn0 setTitleColor:kBlackColor forState:UIControlStateNormal];
    boardBtn0.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    boardBtn0.layer.cornerRadius = kScaleNum(5);
    boardBtn0.layer.masksToBounds = YES;
    [boardBtn0 setTitle:@"0" forState:UIControlStateNormal];
    [boardBtn0 addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boardBtn0];
    
    UIButton *boardDotBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    boardDotBtn.frame = SARectMake(190, StatusHeight + 175 + 3 * 85, 90, 80);
    boardDotBtn.titleLabel.font = kFont(25);
    boardDotBtn.backgroundColor = [UIColor whiteColor];
    [boardDotBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    boardDotBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    boardDotBtn.layer.cornerRadius = kScaleNum(5);
    boardDotBtn.layer.masksToBounds = YES;
    [boardDotBtn setTitle:@"." forState:UIControlStateNormal];
    [boardDotBtn addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boardDotBtn];
    self.boardDotBtn = boardDotBtn;
    
    UIButton *boardDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    boardDeleteBtn.frame = SARectMake(3 * 95, StatusHeight + 175, 90, 80);
    boardDeleteBtn.titleLabel.font = kFont(25);
    boardDeleteBtn.backgroundColor = [UIColor whiteColor];
    boardDeleteBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    boardDeleteBtn.layer.cornerRadius = kScaleNum(5);
    boardDeleteBtn.layer.masksToBounds = YES;
    [boardDeleteBtn setImage:[UIImage imageNamed:@"pay_bule_garbage"] forState:UIControlStateNormal];
    [boardDeleteBtn addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boardDeleteBtn];
    self.boardDeleteBtn = boardDeleteBtn;
    
    UIButton *boardCloseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    boardCloseBtn.frame = SARectMake(3 * 95, StatusHeight + 175 + 85, 90, 80);
    boardCloseBtn.titleLabel.font = kFont(25);
    boardCloseBtn.backgroundColor = [UIColor whiteColor];
    boardCloseBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    boardCloseBtn.layer.cornerRadius = kScaleNum(5);
    boardCloseBtn.layer.masksToBounds = YES;
    [boardCloseBtn setImage:[UIImage imageNamed:@"pay_bule_close"] forState:UIControlStateNormal];
    [boardCloseBtn addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boardCloseBtn];
    self.boardCloseBtn = boardCloseBtn;
    
    UIButton *boardAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    boardAddBtn.frame = SARectMake(3 * 95, StatusHeight + 175 + 170, 90, 165);
    boardAddBtn.titleLabel.font = kFont(25);
    boardAddBtn.backgroundColor = [UIColor whiteColor];
    boardAddBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    boardAddBtn.layer.cornerRadius = kScaleNum(5);
    boardAddBtn.layer.masksToBounds = YES;
    [boardAddBtn setImage:[UIImage imageNamed:@"pay_bule_add"] forState:UIControlStateNormal];
    [boardAddBtn addTarget:self action:@selector(keyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boardAddBtn];
    self.boardAddBtn = boardAddBtn;
    
    UIButton *settlementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settlementBtn.frame = CGRectMake(0, kScreenHeight - kScaleNum(40) - SafeFooterHeight, kScreenWidth, kScaleNum(40) + SafeFooterHeight);
    [settlementBtn setBackgroundImage:[UIImage imageNamed:@"top_image"] forState:UIControlStateNormal];
    [settlementBtn setTitle:@"结算" forState:UIControlStateNormal];
    settlementBtn.titleLabel.font = kBFont(18);
    [settlementBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [settlementBtn addTarget:self action:@selector(unisversalButtonAcrion:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:settlementBtn];
    self.settlementBtn = settlementBtn;
}

-(SAReceiveSaveAPI *)receiveSaveAPI {
    if (!_receiveSaveAPI) {
        _receiveSaveAPI= [SAReceiveSaveAPI apiWithDelegate:self];
    }
    return _receiveSaveAPI;
}

-(SAReceiveReceivePayAPI *)receiveReceivePayAPI {
    if (!_receiveReceivePayAPI) {
        _receiveReceivePayAPI = [SAReceiveReceivePayAPI apiWithDelegate:self];
        _receiveReceivePayAPI.isPromptErrorMessage = NO;
    }
    return _receiveReceivePayAPI;
}

-(SAReceiveQueryStatusAPI *)receiveQueryStatusAPI {
    if (!_receiveQueryStatusAPI) {
        _receiveQueryStatusAPI = [SAReceiveQueryStatusAPI apiWithDelegate:self];
        _receiveQueryStatusAPI.isPromptErrorMessage = NO;
    }
    return _receiveQueryStatusAPI;
}

-(SAGetPayPermissionAPI *)getPayPermissionAPI {
    if (!_getPayPermissionAPI) {
        _getPayPermissionAPI = [SAGetPayPermissionAPI apiWithDelegate:self];
    }
    return _getPayPermissionAPI;
}

-(SASystemSettingsListAPI *)systemSettingsListAPI {
    if (!_systemSettingsListAPI) {
        _systemSettingsListAPI = [SASystemSettingsListAPI apiWithDelegate:self];
    }
    return _systemSettingsListAPI;
}

@end
